﻿using Bomber.TF.Common.GDI;
using Bomber.TF.GameCenter.BomberCenter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bomber.TF.GameCenter.BombCenter
{
    [Species(SpeciesType.Bomb1)]
    public unsafe class Bomb1 : BombBase
    {
        protected static ImageObject[] _iamges;
        static Bomb1()
        {
            Bitmap[] bitmaps = { Properties.Resources.Bomb1_0, Properties.Resources.Bomb1_1, Properties.Resources.Bomb1_2, Properties.Resources.Bomb1_3, Properties.Resources.Bomb1_4, Properties.Resources.Bomb1_5, Properties.Resources.Bomb1_6, Properties.Resources.Bomb1_7, Properties.Resources.Bomb1_8, Properties.Resources.Bomb1_9, Properties.Resources.Bomb1_10, Properties.Resources.Bomb1_11, Properties.Resources.Bomb1_12, Properties.Resources.Bomb1_13, Properties.Resources.Bomb1_14, Properties.Resources.Bomb1_15, Properties.Resources.Bomb1_16, Properties.Resources.Bomb1_17, Properties.Resources.Bomb1_18, Properties.Resources.Bomb1_19, Properties.Resources.Bomb1_20, Properties.Resources.Bomb1_21, Properties.Resources.Bomb1_22 };
            SetImages(bitmaps);
        }
        public Bomb1(BomberBase bomberBase) : base(SpeciesType.Bomb1, new Size(70, 70), bomberBase)
        {

        }
        private int _index = 0;
        private DateTime _date = DateTime.MinValue;
        private DateTime _date2 = DateTime.MinValue;
        public override unsafe void Business(GameListBase* ptr)
        {
            if (_date == DateTime.MinValue)
                _date = DateTime.Now;
            if (_date2 == DateTime.MinValue)
                _date2 = DateTime.Now;
            if (_index == 18)
            {
                if ((DateTime.Now - _date).TotalSeconds >= 3)
                {
                    ++_index;
                    CreateBomb(ptr);
                }
            }
            else
            {
                if (_index < 18)
                {
                    if ((DateTime.Now - _date2).TotalMilliseconds >= 140)
                    {
                        ++_index;
                        _date2 = DateTime.Now;
                    }
                }
                else
                {
                    if (_index == 19) // 判断炸弹是否炸到
                    {
                       base.Attack(ptr);
                    }
                    if (_index >= _iamges.Length)
                    {
                        _index = -11;
                        // 移除
                        var p = ptr + this.Row * base.MaxColumn + base.Column;
                        p->Games.Remove(this);
                    }
                    ++_index;
                }
            }
        }
        public override void Draw(Graphics g)
        {
            if (_index==-10 || _index >= _iamges.Length) return;
            IntPtr hdc = g.GetHdc();
            var img = _iamges[_index];
            GDI32.DrawImageTransparenc(hdc, img.BitmapPtr, Location.X + (CellWidth - Size.Width >> 1) - (int)(7 * ZoomX), Location.Y + (CellHeight - Size.Height >> 1) - (int)(6 * ZoomY), img.Width, img.Height, Size.Width, Size.Height);
            g.ReleaseHdc(hdc);

        }
        protected static void SetImages(Bitmap[] iamges)
        {
            ImageObject[] iamgesObj = new ImageObject[iamges.Length];
            for (int i = 0; i < iamges.Length; i++)
            {
                iamgesObj[i] = new ImageObject(iamges[i]);
            }
            SetImages(iamgesObj);
        }
        protected static void SetImages(ImageObject[] iamges)
        {
            _iamges = iamges;
        }
        protected override void DisposeAll()
        {
            base.DisposeAll();
            foreach (var i in _iamges)
            {
                i.Dispose();
            }
        }
    }
}
